﻿using GalaSoft.MvvmLight.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight.Threading;

namespace UserControls.Utility
{
    public class DialogHelper : IDialogService
    {
        public Task ShowError(Exception error, string title, string buttonText, Action afterHideCallback=null)
        {
            return Task.Run(() =>
            {
                DispatcherHelper.RunAsync(() =>
                {
                    var window = new Common.AlertWindow(title, buttonText, error.Message);
                    window.ShowDialog();
                    afterHideCallback?.Invoke();
                    throw error;
                });
            });
        }

        public Task ShowError(string message, string title, string buttonText, Action afterHideCallback)
        {
            return Task.Run(()=>
            {
                DispatcherHelper.RunAsync(() =>
                {
                    var window = new Common.AlertWindow(title, buttonText, message);
                    window.ShowDialog();
                    afterHideCallback?.Invoke();
                });
            });
        }

        public Task ShowMessage(string message, string title)
        {
            return Task.Run(() =>
            {
                DispatcherHelper.RunAsync(() =>
                {
                    var window = new Common.ConfirmWindow(title, null, message);
                    window.ShowDialog();
                });
            });
        }

        public Task ShowMessage(string message, string title, string buttonText, Action afterHideCallback=null)
        {
            return Task.Run(() =>
            {
                DispatcherHelper.RunAsync(() =>
                {
                    var window = new Common.ConfirmWindow(title, buttonText, message);
                    window.ShowDialog();
                    afterHideCallback?.Invoke();
                });
            });
        }

        public Task<bool> ShowMessage(string message, string title, string buttonConfirmText, string buttonCancelText, Action<bool> afterHideCallback=null)
        {
            return Task.Run<bool>(()=>
            {
                bool? result = false;
                DispatcherHelper.CheckBeginInvokeOnUI(() =>
                {
                    var window = new Common.QuestionWindow(message, title, buttonConfirmText, buttonCancelText);
                    result = window.ShowDialog();
                    afterHideCallback?.Invoke(result.Value);
                });
                return result.Value;
            });
        }

        public Task ShowMessageBox(string message, string title)
        {
            throw new NotImplementedException();
        }
    }
}
